#include "Comm.hpp"
#include "Fifo.hpp"
#include <unistd.h>

int main()
{

    // 1.获取key值
    key_t key = GetShmKeyOrDie();
    cout << "key" << key << endl;

    // 2.创建共享内存
    int shmid = CreateShm(key, defaultsize);
    cout << "shmid: " << shmid << endl;

    // 4.将共享内存和进程进行挂接（关联）
    char *addr = (char *)ShmAttch(shmid);
    cout << "shmat success " << shmid << endl;
    // sleep(2);

    cout << "shmdt success " << shmid << endl;
    // sleep(5);

    // 引入管道
    Fifo fifo;
    Sync syn;
    syn.OpenReadOrDie();

    // 挂接后即可进行通信
    for (;;)
    {
        if(!syn.Wait()) break;
        cout << "shm content " << addr << endl;
    }

    // 3.撤销共享内存
    ShmDetach(addr);
    cout << "delete success " << shmid << endl;

    // 5.删除共享内存
    DeleteShm(shmid);
    return 0;
}